package cn.tianyigps.tianyigpsyndistributetask.task;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Deserializer;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: WEI.YUAN
 * @Descirption:
 * @Date: 2019/4/25_17:51
 */
public class TaskDeserializer implements Deserializer<Task> {

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    @Override
    public void configure(Map<String, ?> map, boolean b) {
    }

    @Override
    public Task deserialize(String s, byte[] bytes) {
        ObjectMapper OM = new ObjectMapper();
        Task task = null;
        try {
            String json= OM.writeValueAsString(bytes);
            JavaType javaType=getCollectionType(Task.class);

            task = OM.readValue(json, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return task;
    }

    @Override
    public void close() {

    }
}
